package org.example.日期与时间;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

public class 新 {

    public static void main(String[] args) {
        // LocalDate 不带时间的日期 如2025-04-05
        LocalDate localDate = LocalDate.now();
        System.out.println("LocalDate: " + localDate);

        // LocalTime 不带日期的时间 如 14:30:00
        LocalTime localTime = LocalTime.now();
        System.out.println("LocalTime: " + localTime);

        // LocalDateTime 不带时区的日期时间 如2025-04-05T14:30:00
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("LocalDateTime: " + localDateTime);

        // ZonedDateTime 表示带时区的完整时间如：2025-04-05T14:30:00+08:00[Asia/Shanghai]
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("ZonedDateTime: " + zonedDateTime);

        // Instant 表示时间戳UTC 时间，从 1970-01-01T00:00:00Z 开始的秒/纳秒数
        Instant instant = Instant.now();
        System.out.println("Instant: " + instant);

        // DateTimeFormatter 用于格式化和解析日期时间，替代 SimpleDateFormat线程安全
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("DateTimeFormatter: " + dateTimeFormatter.format(localDateTime));

        // Duration 表示两个时间点之间的时间量基于秒或纳秒，适用于LocalTime LocalDateTime Instant
        LocalDateTime parse1 = LocalDateTime.parse("2025-06-10 20:39:40", dateTimeFormatter);
        LocalDateTime parse2 = LocalDateTime.parse("2025-06-17 20:39:40", dateTimeFormatter);
        Duration between = Duration.between(parse1, parse2);
        System.out.println("Duration: " + between.toDays());

        // Period 表示两个日期之间的“天、月、年”差异 适用于 LocalDate
        Period period = Period.between(LocalDate.parse("2025-05-10"),LocalDate.parse("2025-05-15"));
        System.out.println("Period: " + period.getDays());

        // ZoneId ZoneOffset  表示时区 ID 或偏移量 Asia/Shanghai08:00
        ZoneId chinaZone = ZoneId.of("Asia/Shanghai");
        ZoneOffset zoneOffset = ZoneOffset.of("+08:00");
        System.out.println("ZoneId: " + chinaZone);
        System.out.println("ZoneOffset: " + zoneOffset);

        // 计算两个日期
        // 根据需求选择 Period（年月日）、Duration（秒/纳秒）、 ChronoUnit（任意单位），它们都线程安全、易读性强、功能丰富
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime old = LocalDateTime.parse("2025-06-09T20:39:40");
        Duration result = Duration.between(old, now); // old <= now
        System.out.println(result.toDays());

    }
}
